using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using TnPlus.Entities.Models;
using TnPlus.Entities.Utils;
using TnPlus.Common;

namespace TnPlus.DAL.Base
{
	public class PortfolioHistoryDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public PortfolioHistoryDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the PortfolioHistory table.
		/// </summary>
		public virtual void Insert(PortfolioHistoryInfo portfolioHistoryInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdPortfolio", portfolioHistoryInfo.IdPortfolio),
				new SqlParameter("@PostDate", portfolioHistoryInfo.PostDate),
				new SqlParameter("@IdCommon", portfolioHistoryInfo.IdCommon),
				new SqlParameter("@AccountNo", portfolioHistoryInfo.AccountNo),
				new SqlParameter("@SecSymbol", portfolioHistoryInfo.SecSymbol),
				new SqlParameter("@SecType", portfolioHistoryInfo.SecType),
				new SqlParameter("@Unit", portfolioHistoryInfo.Unit),
				new SqlParameter("@PriceMkt", portfolioHistoryInfo.PriceMkt),
				new SqlParameter("@IMStock", portfolioHistoryInfo.IMStock),
				new SqlParameter("@IMCustomer", portfolioHistoryInfo.IMCustomer),
				new SqlParameter("@AdjustRate", portfolioHistoryInfo.AdjustRate)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "PortfolioHistory_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the PortfolioHistory table.
		/// </summary>
		public virtual void Update(PortfolioHistoryInfo portfolioHistoryInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdPortfolio", portfolioHistoryInfo.IdPortfolio),
				new SqlParameter("@PostDate", portfolioHistoryInfo.PostDate),
				new SqlParameter("@IdCommon", portfolioHistoryInfo.IdCommon),
				new SqlParameter("@AccountNo", portfolioHistoryInfo.AccountNo),
				new SqlParameter("@SecSymbol", portfolioHistoryInfo.SecSymbol),
				new SqlParameter("@SecType", portfolioHistoryInfo.SecType),
				new SqlParameter("@Unit", portfolioHistoryInfo.Unit),
				new SqlParameter("@PriceMkt", portfolioHistoryInfo.PriceMkt),
				new SqlParameter("@IMStock", portfolioHistoryInfo.IMStock),
				new SqlParameter("@IMCustomer", portfolioHistoryInfo.IMCustomer),
				new SqlParameter("@AdjustRate", portfolioHistoryInfo.AdjustRate)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "PortfolioHistory_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the PortfolioHistory table by its primary key.
		/// </summary>
		public virtual void Delete(Guid? idPortfolio)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdPortfolio", idPortfolio)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "PortfolioHistory_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the PortfolioHistory table.
		/// </summary>
		public virtual PortfolioHistoryInfo Select(Guid? idPortfolio)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdPortfolio", idPortfolio)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "PortfolioHistory_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakePortfolioHistoryInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the PortfolioHistory table.
		/// </summary>
		public List<PortfolioHistoryInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "PortfolioHistory_SelectAll"))
			{
				List<PortfolioHistoryInfo> portfolioHistoryInfoList = new List<PortfolioHistoryInfo>();
				while (dataReader.Read())
				{
					portfolioHistoryInfoList.Add(MakePortfolioHistoryInfo(dataReader));
				}

				return portfolioHistoryInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the PortfolioHistory class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual PortfolioHistoryInfo MakePortfolioHistoryInfo(SqlDataReader dataReader)
		{
			PortfolioHistoryInfo portfolioHistoryInfo = new PortfolioHistoryInfo();
			portfolioHistoryInfo.IdPortfolio = SqlClientUtility.GetGuid(dataReader, DbConstants.PORTFOLIOHISTORY.IDPORTFOLIO);
			portfolioHistoryInfo.PostDate = SqlClientUtility.GetDateTime(dataReader, DbConstants.PORTFOLIOHISTORY.POSTDATE);
			portfolioHistoryInfo.IdCommon = SqlClientUtility.GetString(dataReader, DbConstants.PORTFOLIOHISTORY.IDCOMMON);
			portfolioHistoryInfo.AccountNo = SqlClientUtility.GetString(dataReader, DbConstants.PORTFOLIOHISTORY.ACCOUNTNO);
			portfolioHistoryInfo.SecSymbol = SqlClientUtility.GetString(dataReader, DbConstants.PORTFOLIOHISTORY.SECSYMBOL);
			portfolioHistoryInfo.SecType = SqlClientUtility.GetString(dataReader, DbConstants.PORTFOLIOHISTORY.SECTYPE);
			portfolioHistoryInfo.Unit = SqlClientUtility.GetInt32(dataReader, DbConstants.PORTFOLIOHISTORY.UNIT);
			portfolioHistoryInfo.PriceMkt = SqlClientUtility.GetDecimal(dataReader, DbConstants.PORTFOLIOHISTORY.PRICEMKT);
			portfolioHistoryInfo.IMStock = SqlClientUtility.GetDecimal(dataReader, DbConstants.PORTFOLIOHISTORY.IMSTOCK);
			portfolioHistoryInfo.IMCustomer = SqlClientUtility.GetDecimal(dataReader, DbConstants.PORTFOLIOHISTORY.IMCUSTOMER);
			portfolioHistoryInfo.AdjustRate = SqlClientUtility.GetDecimal(dataReader, DbConstants.PORTFOLIOHISTORY.ADJUSTRATE);

			return portfolioHistoryInfo;
		}

		#endregion
	}
}
